﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;

namespace 无线打分网站.Manage
{
    public partial class TeacharEdit : System.Web.UI.Page
    {
        UserExData ued;
        string strid;
        protected void Page_Load(object sender, EventArgs e)
        {
            strid = Request.QueryString["id"];
            if (!IsPostBack)
            {
                DataTable exdt = ExperimentDataDBOption.DataTableSelect();
                if (strid != null && strid != "")//修改
                {
                    TextBox2.ReadOnly = true;
                    Guid id = new Guid(strid);
                    ued = UserExDataDBOption.GetUserExData(id);                    
                    MembershipUser user = Membership.GetUser(id);
                    TextBox2.Text = user.UserName;
                    TextBox1.Text = ued.Name;
                    string[] exss = ued.ExperimentID.Split(';');
                    ListBox1.Items.Clear();
                    ListBox2.Items.Clear();
                    foreach (string ex in exss)
                    {
                        if (string.IsNullOrEmpty(ex))
                            continue;
                        ExperimentData ed = ExperimentDataDBOption.GetExperimentData(new Guid(ex));
                        ListItem li = new ListItem();
                        li.Text = ed.Name;
                        li.Value = ed.ID.ToString();
                        ListBox1.Items.Add(li);
                    }
                    bool bis;
                    foreach (DataRow dr in exdt.Rows)
                    {
                        ListItem li = new ListItem();
                        li.Text = dr["Name"].ToString();
                        li.Value = dr["ID"].ToString();
                        bis=false;
                        foreach(ListItem li1 in ListBox1.Items)
                        {
                            if (li.Text == li1.Text)
                            {
                                bis = true;
                                break;
                            }
                        }
                        if(!bis)
                            ListBox2.Items.Add(li);
                    }
                }
                else
                {
                    ListBox1.Items.Clear();
                    foreach (DataRow dr in exdt.Rows)
                    {
                        ListItem li = new ListItem();
                        li.Text = dr["Name"].ToString();
                        li.Value = dr["ID"].ToString();
                        ListBox2.Items.Add(li);
                    }
                    Button1.Visible = false;
                }
            }

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            ued = new UserExData();
            ued.Name = TextBox1.Text;
            ued.ExperimentID="";
            foreach (ListItem li in ListBox1.Items)
            {
                ued.ExperimentID = ued.ExperimentID + li.Value + ";";
            }
            ued.ExperimentID = ued.ExperimentID.Substring(0, ued.ExperimentID.Length - 1);
            if (strid != null && strid != "")//修改
            {                
                ued.ID = new Guid(strid);
                UserExDataDBOption.Update(ued);               
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('修改成功');GetDataAndClose();</script>");//
            }
            else
            {
                MembershipUser user;
                user = Membership.CreateUser(TextBox2.Text, "888888");
                Roles.AddUserToRole(user.UserName, "用户");
                ued.ID = (Guid)user.ProviderUserKey;
                UserExDataDBOption.Insert(ued);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('添加成功');GetDataAndClose(); </script>");
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            if (ListBox2.SelectedIndex != -1)
            {
                ListBox1.Items.Add(ListBox2.SelectedItem);
                ListBox2.Items.Remove(ListBox2.SelectedItem);
                ListBox1.SelectedIndex = -1;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MembershipUser user = Membership.GetUser(TextBox2.Text);            
            string p = user.ResetPassword();
            user.ChangePassword(p, "888888");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('重置密码成功'); </script>");
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex != -1)
            {
                ListBox2.Items.Add(ListBox1.SelectedItem);
                ListBox1.Items.Remove(ListBox1.SelectedItem);
                ListBox1.SelectedIndex = -1;
            }
        }
    }
}